var app = getApp();
Page({

  data: {
	  cdnUrl: '',
	  userInfo: null,
	  
	  /* 地图参数 */
	  latitude: 0,
	  longitude: 0,
		centerLatitude: 25.27295,
		centerLongitude: 110.28419,
	  windowHeight: app.globalData.windowHeight*2,
	  pattern: "map",// 默认订单列表为map地图模式，list为列表模式
	  systemOrder: true, //系统派单弹窗
	  /* 自定义导航栏参数 */
	  loading: false,
	  color: '#FFFFFF',
	  background: '#02B3BA',
	  show: true,
	  animated: false,
	  back: false,
	  title: '地图',
	  
	  /* 自定义tabbar */
	  list: [
	  	{
	  	    "pagePath": "/page/index/index",
	  	    "iconPath": "/assets/images/tabbar/index.png",
	  	    "selectedIconPath": "/assets/images/tabbar/index-hl.png",
	  	    "text": "首页"
	  	},
	  	{
	  	    "pagePath": "/page/map/index",
	  	    "iconPath": "/assets/images/tabbar/store.png",
	  	    "selectedIconPath": "/assets/images/tabbar/store-hl.png",
	  	    "text": "地图"
	  	},
	  	{
	  	    "pagePath": "/page/discover/index",
	  	    "iconPath": "/assets/images/tabbar/addon.png",
	  	    "selectedIconPath": "/assets/images/tabbar/addon-hl.png",
	  	    "text": "发现"
	  	},
	  	{
	  	    "pagePath": "/page/my/index",
	  	    "iconPath": "/assets/images/tabbar/my.png",
	  	    "selectedIconPath": "/assets/images/tabbar/my-hl.png",
	  	    "text": "我的"
	  	}
	  ],
	  currentTab: 1,
		
		riderList: []
  },
  tabChange(e) {
	  this.setData({
	  	currentTab: e.detail.index
	  })
  	wx.reLaunch({
  	  url: e.detail.item.pagePath
  	})
  	
  },
	riderList: function (lat, lng) {
	  var that = this;
	  app.request('/addons/cms/wxapp.user/riderList', {lat:lat,lng:lng}, function (data, ret) {
	    that.setData({
	      riderList: data,
	    });
			if(data.length>0){
				// console.log(data[0].latitude);return false;
				that.setData({
					centerLatitude: data[0].latitude,
					centerLongitude: data[0].longitude,
				});
			}else{
				that.setData({
					centerLatitude: that.data.latitude,
					centerLongitude: that.data.longitude,
				});
			}
	  }, function (data, ret) {
	    app.error(ret.msg);
	  });
	},
  /* 跳转下单页面 */
  order: function (e){
		var that = this;
  	wx.navigateTo({
  	  url: '/page/booking_order/index',
  	})
  },
  onLoad: function () {
		var that = this;
		if (!app.globalData.userInfo) {
		  app.check(that);
		}
    that.setData({
      cdnUrl: app.cdnUrl,
      userInfo: app.globalData.userInfo
    });
		var lat = that.data.latitude;
    var lng = that.data.longitude;
    console.log(2222222222222222);
		/* 地图组件渲染 */
		wx.getLocation({
			type: 'gcj02',
			success: function(res) {
        console.log(2222222222222222);
        that.riderList(res.latitude, res.longitude);
				that.setData({
					latitude: res.latitude,
					longitude: res.longitude,
					// centerLatitude: res.latitude,
					// centerLongitude: res.longitude,
				})
			}
		})
		that.mapCtx = wx.createMapContext('myMap');
  },

})